// 用户模块路由处理函数

// 引入数据库模块
const db = require('../mysql/index')
// 密码加密模块
const bcrypt = require('bcryptjs')
// 引入jwt
const jwt = require('jsonwebtoken')
const secretKey = 'rent_car'    // 密钥

const commonFunction = require('../common/common')

// 后台





// web端
// 创建充值订单
exports.orderPay = (req, res) => {
    const { user_id, deposit, order_type } = req.body
    // 生成订单id
    const order_id = commonFunction.order_id()
    const sql = 'INSERT INTO orders VALUES(?, ?, ?, ?)'
    db.query(sql, [order_id, 'during', user_id, order_type], (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc('订单创建失败')
        db.query(`UPDATE user set deposit = deposit + ${deposit} WHERE id = ${user_id}`, (err1, result1) => {
            if (err1) return res.cc(err1)
            db.query(`UPDATE orders set order_status = 'success' WHERE user_id = ${user_id}`, (err2, result2) => {
                if (err2) return res.cc(err2)
                return res.send({
                    code: 200,
                    order_id
                })
            })
        })
    })
}
// 查询订单状态
exports.orderStatus = (req, res) => {
    const order_id = req.body.order_id
    const sql = 'SELECT order_status from orders WHERE order_id = ?'
    db.query(sql, order_id, (err, result) => {
        if (err) return res.cc(err)
        if (result.length !== 1) return res.cc('未查询到该订单')
        return res.send({
            code: 200,
            order_status: result[0].order_status
        })
    })
}
// 创建租车订单


